<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>js常用方法集</title>
		<script src="index.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<p>测试用例，打开控制台，点击对应的按钮，在控制台查看输出</p>
		<button onclick="mergeObjectTest()">mergeObjectTest</button>
		<script type="text/javascript">
			function mergeObjectTest() {
				let obj1 = {
					name: "张三",
					age: 18,
				}
				let obj2 = {
					name: "张三",
					sex: "男",
					eat: function() {
						console.log(this.name + "吃饭");
					},
					study: [{
							subject: "语文",
							score: 80
						},
						{
							subject: "数学",
							score: 90
						},
					]
				}
				// 因为对象是引用类型，所以console.log看不出合并前后的变化，这时候对对象做一层浅拷贝再输出 就能看到效果了
				console.log({
					type: "合并前",
					obj1: {
						...obj1
					},
					obj2: {
						...obj2
					}
				});
				try {
					obj1.eat()
				} catch (e) {
					//TODO handle the exception
					console.error(e)
					console.log(e)
				}
				mergeObject(obj1, obj2)
				console.log({
					type: "合并后",
					obj1: {
						...obj1
					},
					obj2: {
						...obj2
					}
				});
				obj1.eat()
				// 如果参数类型不是对象或者数组，则报错
				let a = 1
				let b = 2
				console.log({
					type: "合并前",
					a,
					b
				});
				try {
					mergeObject(a, b)
				} catch (e) {
					//TODO handle the exception
					console.log(e);
				}
				console.log({
					type: "合并后",
					a,
					b
				});
				let c = []
				let d = [1, 2, 3, 4, 5, [1, 23, 3]]
				mergeObject(c, d)
				console.log({
					type: "合并后",
					c,
					d
				});
			}
		</script>
	</body>
</html>
